<?php 
function mp_timetable_results(){
	global $wpdb;
	$table_name = $wpdb->prefix . "mf_timetable";
	$query="SELECT * FROM " . $table_name;
	$results = $wpdb->get_results($query);
	?><form name="timetable_delete" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>"><input type="hidden" name="mf_timetable_delete_hidden" value="Y"><table class="mf_timetable_admin"><?php
	foreach ($results as $results){
		?><tr><?php
		?><td><?php
		echo $results->day;
		?></td><?php
		?><td><?php
		echo $results->class;
		?></td><?php
		?><td><?php
		echo $results->location;
		?></td><?php
		?><td><?php
		echo $results->start;
		?></td><?php
		?><td><?php
		echo $results->end;
		?></td><?php
		?><td><?php
		echo $results->teacher;
		?></td><?php
		?><td>£<?php
		echo $results->cost;
		?></td><?php
		?><td><?php
		echo $results->category;
		?></td><td><input type="checkbox" name="<?php echo $results->class; ?>" value="<?php echo $results->id; ?>" /></td></tr><?php
	}
	?></table><input type="submit" name="delete selected" value="<?php _e('delete selected', 'mf_timetable_delete' ) ?>" /></form><?php
}
function mp_timetable_delete(){
	global $wpdb;
	$table_name = $wpdb->prefix . "mf_timetable";
	echo implode(",", $_POST);
	foreach ($_POST as $list){
		
		
		$sql= "DELETE FROM " . $table_name .
			   " WHERE id = " . $list;
		$test=$wpdb->query($sql);
		
	};
	
	?>
	<div class="updated"><p><strong><?php _e('Event Deleted.'); ?></strong></p></div>
	<?php
}
function mp_timetable_update(){
	global $wpdb;
	$table_name = $wpdb->prefix . "mf_timetable";
	//Form data sent
	$mf_timetable_new_day = $_POST['mf_timetable_day'];
	
	
	$mf_timetable_new_class = $_POST['mf_timetable_class'];
	
	
	$mf_timetable_new_location = $_POST['mf_timetable_location'];
	
	
	$mf_timetable_new_start = $_POST['mf_timetable_start'];
	
	
	$mf_timetable_new_end = $_POST['mf_timetable_end'];
	
	
	$mf_timetable_new_teacher = $_POST['mf_timetable_teacher'];

	
	$mf_timetable_new_cost = $_POST['mf_timetable_cost'];
	
	
	$mf_timetable_new_category = $_POST['mf_timetable_category'];
	
	$query= "INSERT INTO " . $table_name .
        " (day, class, location, start, end, teacher, cost, category) " .
        "VALUES ('$mf_timetable_new_day','$mf_timetable_new_class','$mf_timetable_new_location','$mf_timetable_new_start','$mf_timetable_new_end','$mf_timetable_new_teacher','$mf_timetable_new_cost','$mf_timetable_new_category')";
	
	$test=$wpdb->query($query);
	
	
	?>
	<div class="updated"><p><strong><?php _e('Event Added.'); ?></strong></p></div>
	<?php	
};


	if($_POST['oscimp_hidden'] == 'Y') {
		mp_timetable_update();
	} elseif ($_POST['mf_timetable_delete_hidden'] == 'Y') {
		mp_timetable_delete();
	};
?>


<div class="wrap">
	
	<?php    echo "<h2>" . __( 'Fuzzy Timetable Admin ', 'oscimp_trdom' ) . "</h2>"; ?>
	
	<form name="timetable_input" method="post" action="<?php echo str_replace( '%7E', '~', $_SERVER['REQUEST_URI']); ?>">
		<input type="hidden" name="oscimp_hidden" value="Y">
		<?php    echo "<h4>" . __( 'Add New Event', 'mf_timetable_trdom' ) . "</h4>"; ?>
		
		<p><?php _e("Day" ); ?>
		<select name="mf_timetable_day">
		<option value="mon">Monday</option>
		<option value="tue">Tuesday</option>
		<option value="wed">Wednesday</option>
		<option value="thu">Thursday</option>
		<option value="fri">Friday</option>
		<option value="sat">Saturday</option>
		<option value="sun">Sunday</option>
		</select></p>
		
		<p><?php _e("Class Name: " ); ?><input type="text" name="mf_timetable_class" value="" size="20"><?php _e("" ); ?></p>
		
			<p><?php _e("Category" ); ?>
			<select name="mf_timetable_category">
			<option value="ballet">Ballet</option>
			<option value="modern">Modern/Jazz</option>
			<option value="tap">Tap</option>
			<option value="adult">Adult</option>
			
			</select></p>
		
		<p><?php _e("Location: " ); ?><input type="text" name="mf_timetable_location" value="" size="20"><?php _e("" ); ?></p>
		<p><?php _e("Start Time: " ); ?><input type="text" name="mf_timetable_start" value="" size="20"><?php _e("" ); ?></p>
		<p><?php _e("End Time: " ); ?><input type="text" name="mf_timetable_end" value="" size="20"><?php _e("" ); ?></p>
		<p><?php _e("Teacher: " ); ?><input type="text" name="mf_timetable_teacher" value="" size="20"><?php _e("" ); ?></p>
		<p><?php _e("Cost: £" ); ?><input type="text" name="mf_timetable_cost" value="" size="20"><?php _e("" ); ?></p>
		<p class="submit">
		<input type="submit" name="Submit" value="<?php _e('add event', 'mf_timetable_trdom' ) ?>" />
		</p>
	</form>
	<hr />
	<?php    echo "<h4>" . __( 'Current Listings', 'mf_timetable_trdom' ) . "</h4>";?>
	
	<?php mp_timetable_results();?>
</div>
